<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class messagecontroller extends Controller
{
    //添加分享
    public function add()
    {
    	return view('home.message.add', ['title' => '分享发表']);
    }
    //插入分享
    public function insert(Request $request)
    {
         $this -> validate($request,[
              'mpic'=>'required|image|max:2100'
            ]);


        $data = $request -> except('_token');   //请求页面上除去token之外的所有值
        $data['uname'] = session('homeM')->uname;  //获取全局session下的uname
        // $uid = $request -> input('uid');
        $time = Date('Y-m-d H:i:s');   //获取时间戳
        $data['regtime'] = $time;     //添加时间
       

        //处理上传图片
        if($request -> hasFile('mpic'))
        {
		    if($request -> file('mpic') -> isValid())
		    {
		        $extension = $request -> file('mpic') -> extension();
		        $filename = mt_rand(1000,9999).'.'.$extension;
		        $dir = './uploads/avatar/';
		        $request -> file('mpic') -> move($dir,$filename);
		        $data['mpic'] = '/uploads/avatar/'.$filename;
		    }
        }else
        {
        	
        	$data['mpic'] = './uploads/avatar/default.jpg';
        }


        $res = DB::table('message')->insert($data);   //将所有值添加到数据库
        if($res)
        {
        	return redirect('/home/message/index') -> with(['info'=>'添加成功']);
        }else
        {
        	return back() -> with(['info'=>'添加失败']);
        } 

    } 

    //展示分享
    public function index()
    {
		
		$data = DB::table('message') ->get();

    	return view('home.message.index', ['data' => $data, 'title' => '留言展示']);
    }

    //插入评价评价
    public function review($uid)
    {
    	
    	$data = DB::table('message')
    	 ->where('uid',$uid)
    	 ->first();       //对象   没有k值
    	 $res = DB::table('discuss')
    	 ->where('uid',$uid)
    	 -> get();        //数组的形式   有k值

    	return view('home.message.review', ['data' => $data, 'res' => $res, 'title' => '添加评论']); 
    	//'res' => $res,   将res的数组中的值发送到页面   
    }
   
    public function update(Request $request)
    { 
    	$data = $request->except('_token');

    	$data['dregtime'] = Date('Y-m-d H:i:s');
    	// dd($data);
        //$data['pid'] = session('homeM')->uid; 
    	$res = DB::table('discuss') -> insert($data);
    	if($res)
        {
        	return redirect('/home/message/review'.'/'.$data['uid']) -> with(['info'=>'添加成功']);
        	//查询跳转到的页面  此处是加参数是回到当前和uid匹配的页面
        }else
        {
        	return back() -> with(['info'=>'添加失败']);
        } 
    }
    public function sAdd(Request $request)
    {

        $uid = $request -> uid;
        $pid = $request -> pid;
        $comment = $request -> comment;
        $data['uid'] = $uid;
        $data['uname'] = session('homeM')->uname;
        $data['pid'] = $pid;
        $data['sid'] = session('homeM')->uid;
        $data['comment'] = $comment;
        $data['dregtime'] = Date('Y-m-d H:i:s');
        Session::put('pid',$pid);
        $res = DB::table('discuss')-> insert($data); 
        if ($res) 
        {
           return 1;
        }else
        {
           return 0;
        }
    }
    
}